<?php

namespace App\Console\Commands;

use Exception;
use App\Models\System\Setting;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\Attendance\Integral;

class AttendanceIntegral extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'attendance:integral';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $date = date('Y-m-d', strtotime("-1 day"));
        $data = DB::table('schedules as s')
            ->leftJoin('attendances as a', 'a.schedule_id', '=', 's.id')
            ->select(['a.time_difference', 'a.checkin_status', 'a.checkout_status', 's.company_id', 'a.user_id', 'a.project_id', 's.date'])
            ->where('s.date', $date)
            ->orderByDesc('a.time_difference')
            ->get();
        $data = $data->groupBy('company_id');
        $data = $data->toArray();
        DB::beginTransaction();
        try{
            foreach ($data as $k => $v) {
                $rules = Setting::query()->where('company_id', $k)->value('attendance') ? json_decode(Setting::query()->where('company_id', $k)->value('attendance'), true) : config('system.attendance');
                foreach ($v as $key => $val) {
                    $this->checkin_status($val, $key, $rules);
                }
            }
            DB::commit();
        }catch(Exception $e){
            DB::rollBack();
           $this->error($e->getMessage());
        }

        DB::commit();
        $this->info('Success');
    }

    private function checkin_status($val, $key, $rules)
    {
        /*签到状态正常begin*/
        if ($val->checkin_status == 'normal') {
            $insert = [
                'user_id' => $val->user_id,
                'project_id' => $val->project_id,
                'date' => $val->date,
                'created_at' => date('Y-m-d H:i:s', time()),
                'updated_at' => date('Y-m-d H:i:s', time()),
            ];
            //第一名
            if ($key == 0) {
                $insert['score'] = $rules['first'];
                $insert['note'] = 'first';
                Integral::query()->create($insert);
            }
            //第二名
            if ($key == 1) {
                $insert['score'] = $rules['second'];
                $insert['note'] = 'second';
                Integral::query()->create($insert);
            }
            //第三名
            if ($key == 2) {
                $insert['score'] = $rules['third'];
                $insert['note'] = 'third';
                Integral::query()->create($insert);
            }

            //最后一名
            if ($key == (collect($val)->count()) - 1) {
                $insert['score'] = $rules['last'];
                $insert['note'] = 'last';
                Integral::query()->create($insert);
            }
            $this->checkout_status($val, $rules);
        }
        /*签到状态正常end*/

        /*签到状态迟到begin*/
        if ($val->checkin_status == 'tardy') {
            $insert = [
                'user_id' => $val->user_id,
                'project_id' => $val->project_id,
                'date' => $val->date,
                'note' => 'tardy',
                'score' => $rules['tardy'],
                'created_at' => date('Y-m-d H:i:s', time()),
                'updated_at' => date('Y-m-d H:i:s', time()),
            ];
            Integral::query()->create($insert);

            //最后一名
            if ($key == (collect($val)->count()) - 1) {
                $insert3 = [
                    'user_id' => $val->user_id,
                    'project_id' => $val->project_id,
                    'date' => $val->date,
                    'note' => 'last',
                    'score' => $rules['last'],
                    'created_at' => date('Y-m-d H:i:s', time()),
                    'updated_at' => date('Y-m-d H:i:s', time()),
                ];
                Integral::query()->create($insert3);
            }
            $this->checkout_status($val, $rules);

        }
        /*签到状态迟到end*/

        /*签到状态为旷工或者空begin*/
        if ($val->checkin_status == 'absenteeism' || $val->checkin_status == null) {
            $insert = [
                'user_id' => $val->user_id,
                'project_id' => $val->project_id,
                'date' => $val->date,
                'note' => 'absenteeism',
                'score' => $rules['absenteeism'],
                'created_at' => date('Y-m-d H:i:s', time()),
                'updated_at' => date('Y-m-d H:i:s', time()),
            ];
            Integral::query()->create($insert);
        }
        /*签到状态为旷工或者空end*/
    }

    private function checkout_status($val, $rules)
    {
        /*签退状态早退begin*/
        if ($val->checkout_status == 'early') {
            $insert = [
                'user_id' => $val->user_id,
                'project_id' => $val->project_id,
                'date' => $val->date,
                'note' => 'early',
                'score' => $rules['early'],
                'created_at' => date('Y-m-d H:i:s', time()),
                'updated_at' => date('Y-m-d H:i:s', time()),
            ];
            Integral::query()->create($insert);
        }
        /*签退状态早退end*/
    }
}
